﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Ajax.Utilities;
using MyWorkBlogDAL;
using MyWorkBlogMVC.Models;
using PagedList;
using PagedList.Mvc;
using Microsoft.AspNet.Identity;

namespace MyWorkBlogMVC.Controllers
{
    public class UserController : Controller
    {
        Blog userBlog = new Blog();
        private Blog user;

        public UserController()
        {
            user = System.Web.HttpContext.Current.Session["User"] as Blog;
        }

        //
        // GET: /User/

        public ActionResult UserDetails()
        {

            if (System.Web.HttpContext.Current.Session["User"] != null)
            {
                userBlog = Blog.GetBlog(user.Username);

                var bPost = BlogPost.GetBlogPostlist(userBlog.BlogId);

                var model = new UserBlogModel
                {

                    Name = userBlog.Name,
                    Country = userBlog.Country,
                    Description = userBlog.Description,
                    Username = userBlog.Username,
                    BlogPosts = bPost
                };

                return View(model);
            }

            else
            {
                return RedirectToAction("Login", "Account");
            }
        }


        public ActionResult BlogEdit()
        {
            var blog = Blog.GetBlog(user.Username.Trim());
            var model = new BlogEditModel()
            {
                Name = blog.Name,
                Description = blog.Description,
                Username = blog.Username,
                Country = blog.Country
            };
            return View(model);
        }


        [HttpPost]
        public ActionResult BlogEdit(BlogEditModel model)
        {
            if (Blog.IsValidPassword(user.Username.Trim(), model.Password.Trim()))
            {
                var blog = new Blog
                {
                    BlogId = Blog.GetBlog(user.Username.Trim()).BlogId,
                    Name = model.Name,
                    Description = model.Description,
                    Username = user.Username.Trim(),
                    Country = model.Country,
                    Password = model.NewPassword.Trim()
                };

                if (Blog.UpdateBlog(blog) > 0)
                {
                    //System.Web.Security.FormsAuthentication.SetAuthCookie(user.Username, true);
                    return RedirectToAction("UserDetails", "User");
                }
            }
            return View(model);
        }



        public ActionResult CreateUserPost()
        {
            return View();
        }
        [HttpPost]
        public ActionResult CreateUserPost(CreateUserPostModel model)
        {
            var post = new BlogPost()
            {
                PostContent = model.PostContent,
                PostTitle = model.PostTitle,
                BlogId = Blog.GetBlog(user.Username).BlogId,
                PostDate = DateTime.Now.Date,
                LastModifyDate = DateTime.Now.Date
            };
            if (BlogPost.InsertNewBlogPost(post) > 0)
            {
                return RedirectToAction("UserDetails");
            }
            return View();
        }

        public ActionResult EditUserPostWithComment(int id, int? page)
        {


            var post = BlogPost.GetBlogPostPostWithPostId(id);
            var model = new EditUserPostWithCommentModel()
            {
                BlogPosts = post,
                PostComments = Comment.GetComments(post.BlogPostId).ToPagedList(page ?? 1, 5)
            };


            return View(model);
        }






        public ActionResult DeletePost(int id, int? page)
        {
            var post = BlogPost.GetBlogPostPostWithPostId(id);
            var model = new EditUserPostWithCommentModel()
            {
                BlogPosts = post,
                PostComments = Comment.GetComments(post.BlogPostId).ToPagedList(page ?? 1, 5)
            };
            return View(model);
        }
        [HttpPost]
        public ActionResult DeletePost(EditUserPostWithCommentModel model, int id)
        {
            if (BlogPost.DeleteBlogPostWithId(id) > 0)
            {
                return RedirectToAction("UserDetails");
            }

            return View(model);
        }

        //[HttpPost]
        //public ActionResult DeleteComment(int id)
        //{

        //    var comment = Comment.GetCommentObject(id);
        //    int urlPostId = comment.BlogPostId;
        //    Comment.DeleteComment(comment);
           
        //    if(urlPostId > 0)
        //    return RedirectToAction("EditUserPostWithComment", urlPostId);
        //    return HttpNotFound();


        //}

        [HttpPost]
        public ActionResult _EditUserPostComment(int id)
        {

            var comment = Comment.GetCommentObject(id);
            int urlPostId = comment.BlogPostId;
            Comment.DeleteComment(comment);

            if (urlPostId > 0)
                return RedirectToAction("EditUserPostWithComment/"+urlPostId);
            return HttpNotFound();


        }

        public ActionResult EditPost(int id)
        {
            var post = BlogPost.GetBlogPostPostWithPostId(id);
            var model = new EditUserPostContentModel()
            {
                BlogPostId = post.BlogPostId,
                PostContent = post.PostContent,
                PostTitle = post.PostTitle,
                BlogId = post.BlogId
            };
            return View(model);
        }
    [HttpPost]
        public ActionResult EditPost( EditUserPostContentModel model, int id)
        {
         
            var post = new BlogPost()
            {
                BlogPostId =id,
                PostContent = model.PostContent,
                PostTitle = model.PostTitle
                
               
            };
        if(BlogPost.UpdateBlogPost(post)>0)
            return RedirectToAction("DeletePost","User", new{Id =id});
        return View(model);
        }

    }
}